package com.example.raspberry_pi_demob.service;

import com.example.raspberry_pi_demob.DAO.AllDataDAO;
import com.example.raspberry_pi_demob.entity.AllData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
@Service
public class AllDataService {
    @Autowired
    AllDataDAO allDataDAO;
    public List<AllData> findAllDataList() {
        return allDataDAO.selectAll();
    }

    public AllData findAllDataByTimestamp(Integer timestamp) {
        return allDataDAO.selectByPrimaryKey(timestamp);
    }

    public List<AllData> findAllDataByMacAddress(String macAddress) {
        return allDataDAO.selectByMacAddress(macAddress);
    }


    //需要用到sql的where 大于小于
    public List<AllData> findAllDataByTimeBucket(String startTime, String endTime) {
        String tempStartTime = startTime.replace('_',' ');
        String tempEndTime = endTime.replace('_',' ');
        return allDataDAO.selectByTimeBucket(tempStartTime,tempEndTime);
    }

    public int delete(Integer timestamp) {
        return allDataDAO.deleteByPrimaryKey(timestamp);
    }

    public int save(AllData allData) {
    	Calendar c=Calendar.getInstance();
    	int seconds = allData.getTimestamp();
    	@SuppressWarnings("deprecation")
		long millions=new Long(seconds).longValue()*1000;
    	c.setTimeInMillis(millions);
    	System.out.println(""+c.getTime());
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	String dateString = sdf.format(c.getTime());
    	allData.setDateTime(dateString);
        return allDataDAO.insert(allData);
    }


}
